<?php
/**
 * []
 * @Author: mazhenyu[houdunwangmzy@163.com]
 * @Date:   2014-10-21 10:13:45
 * @Last Modified time: 2014-10-21 11:19:25
 */

$link = @new Mysqli('localhost','root','','moneyinfo');
if($link->connect_errno) die($link->connect_error);
//设置字符集
$link->query('SET NAMES UTF8');
$link->query('BEGIN');
$link->query('SET AUTOCOMMIT=0');
//臧翔明给刘熠转500元
try {
	// 1.臧翔明减去500元
	$link->query('UPDATE m SET money=money-500 WHERE mid=1');
	//抛出错误
	if($link->errno) throw new Exception($link->error);
	//2.刘熠加500
	$link->query('UPDATE m set money=money+500 WHERE mid=2');
	//抛出错误
	if($link->errno) throw new Exception($link->error);
	//如果没有错误，最后要提交
	$link->query('COMMIT');
} catch (Exception $e) {
	echo $e->getMessage();
	//回滚
	$link->query('ROLLBACK');
}

?>